<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class FileBackup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'file:backup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'backup app folders files.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //创建目录
        $upDir = storage_path().'/backups/'.date('Y-m-d', time()).'_backup';
        if(!is_dir($upDir)){
            exec('mkdir -p '.$upDir);
        }

        //删除前三十天
        exec('find '.storage_path().'/backups/ -ctime +30 -name "*_backup" | xargs rm -rf');

        //执行压缩
        exec('tar -zcf '.$upDir.'/public-avatar-'.date('YmdHis', time()).'.tar.gz -C '.public_path().'/avatars .');
        exec('tar -zcf '.$upDir.'/public-files-'.date('YmdHis', time()).'.tar.gz -C '.public_path().'/files .');
        exec('tar -zcf '.$upDir.'/app-files-'.date('YmdHis', time()).'.tar.gz -C '.storage_path().'/app/files .');
        exec('tar -zcf '.$upDir.'/app-uploads-'.date('YmdHis', time()).'.tar.gz -C '.storage_path().'/app/uploads .');
    }
}
